/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.sql.Date;
import model.Enseignant;

/**
 *
 * @author Paul
 */
public class DAOEnseignant extends DAO<Enseignant> {

    public void create(Enseignant obj) {
        String query = "";
        changeGeneral(query);
    }

    public void update(Enseignant obj) {
        String query = "UPDATE `enseignant` SET `id_enseignant`=['"+obj.getId()+"'],`id_personne`=[value-2] WHERE 1";
        changeGeneral(query);
    }

    public void delete(Enseignant obj) {
        String query = "";
        changeGeneral(query);
    }

        public ArrayList<Enseignant> getAll() {
        String query = "";
        return findGeneral(query);
    }

    public Enseignant find(int id) {
        String query = "";
        Enseignant enseignant = null;
        enseignant = findGeneral(query).get(0);
        return enseignant;
    }

    
    
    
    
//---------------------------------------------------------private method--------------------------------------------------------------------//
    private int changeGeneral(String query) {
        Statement st = null;
        int nb = 0;
        try {
            Connection conn = DBConnection.getConnection();
            st = conn.createStatement();
            nb = st.executeUpdate(query);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return nb;
    }

    private ArrayList<Enseignant> findGeneral(String requete) {
        ArrayList<Enseignant> enseignants = new ArrayList<Enseignant>();
        Statement st = null;
        Enseignant enseignant = null;

        try {
            Connection conn = DBConnection.getConnection();
            st = conn.createStatement();
            ResultSet res = st.executeQuery(requete);
            while (res.next()) {
                enseignant = createObject(res);
                enseignants.add(enseignant);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return enseignants;
    }

    private Enseignant createObject(ResultSet res) {
        Enseignant enseignant = null;
        try {
            Integer id = res.getInt("id_personne");
            String nom = res.getString("nom");
            String prenom = null;
            Date datenaissance = null;
            String type = null;
            String login=null;
            enseignant = new Enseignant(id, nom, prenom, datenaissance, type,login, "");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return enseignant;
    }

}
